package commitlog;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;


import sourcecode.*;

public class VisionScore {
	private int pastDays;
	private String getElapsedTimeSting(long startTime) {
		long elapsedTime = System.currentTimeMillis() - startTime;
		String elpsedTimeString = (elapsedTime / 1000) + "." + (elapsedTime % 1000);
		return elpsedTimeString;
	}
	public void create(String pro) throws Exception {
		String sincestr = "2004-06-01";
		String untilstr = "2010-05-01";
		String repoDir = "../gitProject/eclipse.platform.swt/.git";
		if(pro.equals("aspectj")){
			sincestr = "2002-07-01";
			untilstr = "2008-12-15";
			repoDir = "../gitProject/org.aspectj/.git";
		}
		else if(pro.equals("eclipse")){//eclipse commitlog
			sincestr = "2004-08-01";
			untilstr = "2011-03-31";
			repoDir = "../gitProject/eclipse.platform/.git";
		}		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date sinceDate = dateFormat.parse(sincestr);
		Date untilDate = dateFormat.parse(untilstr);
		
		GitCommitLogCollector gitCommitLogCollector = new GitCommitLogCollector(repoDir);
		System.out.printf("Commit log collecting start.\n");
		gitCommitLogCollector.collectCommitLog(sinceDate, untilDate);
		System.out.printf("Commit log collecting finished.\n");
		
	}
	/*public void compute(ArrayList<ExtendedCommitInfo> allCommitInfo, Date openDate, Integer pastDays){
		ArrayList<ExtendedCommitInfo> relatedCommitInfos = findCommitInfoWithinDays(allCommitInfo, openDate, pastDays);
		if (null == relatedCommitInfos) {
			return;
		}
		for (int j = 0; j < relatedCommitInfos.size(); j++) {
			ExtendedCommitInfo relatedCommitInfo = relatedCommitInfos.get(j);
			HashSet<String> commitFiles = relatedCommitInfo.getAllCommitFilesWithoutCommitType();
			Iterator<String> commitFilesIter = commitFiles.iterator();
			while (commitFilesIter.hasNext()) {
				String commitFileName = commitFilesIter.next();

				// Calculate CommitLogScore for file level
				IntegratedAnalysisValue analysisValue = analysisValues.get(commitFileName);
				if (null == analysisValue) {
					analysisValue = new IntegratedAnalysisValue();
					analysisValue.setBugID(bug.getID());
					analysisValue.setVersion(version);
					analysisValue.setFileName(commitFileName);
				}
				
				double commitLogScore = analysisValue.getCommitLogScore();
				commitLogScore += calculateCommitLogScore(relatedCommitInfo.getCommitDate(), bug.getOpenDate(), pastDays);
				analysisValue.setCommitLogScore(commitLogScore);
				
				if (null == analysisValues.get(commitFileName)) {
					analysisValues.put(commitFileName, analysisValue);		
				}
			}
			
			HashMap<String, ArrayList<Method>> allCommitMethods = relatedCommitInfo.getAllFixedMethods();
			if (allCommitMethods == null) {
//				System.err.printf("[NO fixed method!] BugID: %d, Commit ID: %s\n", bug.getID(), relatedCommitInfo.getCommitID());
				continue;
			}
			
			commitFilesIter = allCommitMethods.keySet().iterator();
			while (commitFilesIter.hasNext()) {
				String commitFileName = commitFilesIter.next();
				ArrayList<Method> commitMethods = allCommitMethods.get(commitFileName);
				
				for (int i = 0; i < commitMethods.size(); ++i) {
					Method method = commitMethods.get(i);
					int methodID = methodDAO.getMethodID(method);

					if (methodID == BaseDAO.INVALID) {
						int sourceFileVersionID = sourceFileDAO.getSourceFileVersionID(commitFileName, SourceFileDAO.DEFAULT_VERSION_STRING);
						method.setSourceFileVersionID(sourceFileVersionID);
						methodID = methodDAO.insertMethod(method);
					}

					ExtendedIntegratedAnalysisValue methodAnalysisValue = methodAnalysisValues.get(methodID);
					if (null == methodAnalysisValue) {
						methodAnalysisValue = new ExtendedIntegratedAnalysisValue();
						methodAnalysisValue.setBugID(bug.getID());
						methodAnalysisValue.setMethodID(methodID);
					}

					// Calculate CommitLogScore for method level
					double commitLogScore = methodAnalysisValue.getCommitLogScore();
					commitLogScore += calculateCommitLogScore(relatedCommitInfo.getCommitDate(), bug.getOpenDate(), pastDays);
					methodAnalysisValue.setCommitLogScore(commitLogScore);
					
					if (null == methodAnalysisValues.get(methodID)) {
						methodAnalysisValues.put(methodID, methodAnalysisValue);		
					}
				}
			}
		}
	}*/
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		VisionScore vision = new VisionScore();
		vision.create("aspectj");
	}
	private ArrayList<ExtendedCommitInfo> findCommitInfoWithinDays(ArrayList<ExtendedCommitInfo> allCommitInfo, Date openDate, Integer pastDays) {
		ArrayList<ExtendedCommitInfo> foundCommitInfos = null;
		for (int i = 0; i < allCommitInfo.size(); i++) {
			ExtendedCommitInfo commitInfo = allCommitInfo.get(i);
			
			Date commitDate = commitInfo.getCommitDate();
		    double diffDays = getDiffDays(commitDate, openDate);
			
		    if (diffDays > pastDays) {
		    	continue;
		    }

	        if ((diffDays > 0) && (diffDays <= pastDays)) {
				if (null == foundCommitInfos) {
					foundCommitInfos = new ArrayList<ExtendedCommitInfo>();
				}

				foundCommitInfos.add(commitInfo);						
			} else {
				break;
			}			
		}
		
		return foundCommitInfos;
	}
	private double getDiffDays(Date sourceDate, Date targetDate) {
		long diff = targetDate.getTime() - sourceDate.getTime();
	    double diffDays = diff / (24.0 * 60 * 60 * 1000);
		
	    return diffDays;
	}
}
